package vertx;

import com.hazelcast.config.Config;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.spi.cluster.hazelcast.HazelcastClusterManager;
import netty.util.StartAndStop;
import test.PerformanceAnalyze;

public class BusBridgeClient extends StartAndStop {

    static EventBus eb;

    PerformanceAnalyze performanceAnalyze = new PerformanceAnalyze();

    public void start() {
        Config config = new Config();
        config.getNetworkConfig().setPublicAddress("127.0.0.1").setPort(5702);
        config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true)
                .addMember("127.0.0.1:5701");
        ClusterManager mgr = new HazelcastClusterManager(config);

        VertxOptions options = new VertxOptions().setClusterManager(mgr);

        Vertx.clusteredVertx(options, res -> {
            if (res.succeeded()) {
                Vertx vertx = res.result();
                eb = vertx.eventBus();

            } else {
                // failed!
            }
        });

    }

    public static void main(String[] args) {
        new Thread(() -> {
            while (running) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (eb == null) {
                    continue;
                }
                for (int i = 0; i < 10000; i++) {
                    eb.publish("news.uk.sport", System.nanoTime());
                }
            }
        }).start();

        new BusBridgeClient().start();

    }
}
